<include file="Map:nav" />
<div>
    <div id="mapLeft" class="mapLeft">
        <div class="mapSo">
            <div class="mapSotext">
                <input id="key" type="text" value="请输入楼盘名称搜索" >
                <a href="javascript:;" id="so_but" class="btn but38">查找</a> </div>
            <div class="dropdownBox"> <span>
        <select id="area_id">
            <volist name='area_list' id='vo'>
                <option value="{$vo.id}">{$vo.name}</option>
            </volist>
        </select>
        </span> <span>
        <select id="area_ids"></select>
        </span> <span>
        <select id="room" class="s_w">
            <option value="0">室</option>
            <volist name='menu[24]' id='vo'>
                <option value="{$vo.parameter}">{$vo.name}室</option>
            </volist>
        </select>
        </span> <span>
        <select id="hall" class="s_w">
            <option value="0">厅</option>
            <volist name='menu[24]' id='vo'>
                <option value="{$vo.parameter}">{$vo.name}厅</option>
            </volist>
        </select>
        </span>
            <span>
        <select id="kitchen" class="s_w">
            <option value="0">卫</option>
            <volist name='menu[24]' id='vo'>
                <option value="{$vo.parameter}">{$vo.name}卫</option>
            </volist>
        </select>
        </span>
                <span>
        <select id="toilet" class="s_w">
            <option value="0">厨</option>
            <volist name='menu[24]' id='vo'>
                <option value="{$vo.parameter}">{$vo.name}厨</option>
            </volist>
        </select>
        </span>
            </div>
        </div>
        <div class="mapListBox">
            <div class="mapListHead">
                <dl>
                    <dt>共找到<b id="HousesCount">0</b>个楼盘</dt>
                    <dd> <input type="hidden" id="sort" value="1"><span><a class="mapSort" data-did='1' href="javascript:;" >最新</a></span> <span><a class="mapSort" data-did='3' href="javascript:;">推荐</a></span> <span><a class="mapSort" data-did='7' href="javascript:;">价格</a></span> </dd>
                </dl>
            </div>
            <div class="mapList" style="height: 465px;">
                <ul id="ulContent"></ul>
            </div>
        </div>
    </div>
    <div id="mapRight" class="mapRight" style="position: relative"></div>
</div>

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak={$ak}"></script>
<script type="text/javascript" src="__JS__/layer/layer.js"></script>
<script type="text/javascript" src="__JS__/public.js"></script>
<script type="text/javascript">
    $("#key").fangCmsText();
</script>
<script type="text/javascript">
    //javascript的数组扩展方法
    Array.prototype.contains = function (obj) {
        var i = this.length;
        while (i--) {
            if (this[i] === obj) {
                return true;
            }
        }
        return false;
    }
</script>
<script>
    $(document).ready(function () {
        $("#mapWrap").height($(window).height());
        $(window).resize(function () {
            $("#mapWrap").height($(window).height());
        });
        $('#mapLeft,#mapRight').css('height', $(window).height() - 60);
        $(window).resize(function () {
            $('#mapLeft,#mapRight').css('height', $(window).height() - 60);
        });
        $('.mapList').css('height', $(window).height() - 258);
        $(window).resize(function () {
            $('.mapList').css('height', $(window).height() - 258);
        });
        getAreaCate();
    });
    $("#so_but").click(function(){
        if($("#key").val()!='请输入楼盘名称搜索'){
            getAreaCate();
        }else{
            msg_show('请输入楼盘名称搜索或关键词',2);
        }
    });
</script>
<script type="text/javascript">
    var height = document.documentElement.clientHeight;
    var width = document.documentElement.clientWidth;
    var lvjupath ="http://" + window.location.host;
    var houseID = ""; //缓存已经加载过的楼盘ID，以字符串形式，ID间用逗号相隔
    // 百度地图API功能
    var mp = new BMap.Map("mapRight", { enableMapClick: false });
    mp.centerAndZoom(new BMap.Point(110.31327, 20.030473), 15);
    mp.addControl(new BMap.NavigationControl());               // 添加平移缩放控件
    mp.addControl(new BMap.ScaleControl());                    // 添加比例尺控件

    mp.addControl(new BMap.OverviewMapControl());              // 添加缩略地图控件
    mp.enableScrollWheelZoom(); 							   // 启用滚轮放大缩小
    var positions = new Array("0", "-20", "-10", "-30");
    var backgroundColors = new Array("#33C0D0", "#fc5172", "#ff9600", "#9e9e9e");
    var borders = new Array("#0D5780", "#CC0066", "#FF6600", "#4B4B4B");

    function ComplexCustomOverlay(point_lng, point_lat, text, mouseoverText, type, bid, address, path, phone1, contacts) {
        this._point = new BMap.Point(point_lng, point_lat);
        this._text = text;
        this._overText = mouseoverText;
        this._type = getType(type);
        this._y_type = type;
        this._bid = bid;
        this._point_lng = point_lng;
        this._point_lat = point_lat;
        this._address = address;
        this._path = path;
        this._phone1 = phone1;
        this._contacts = contacts;
    }
    ComplexCustomOverlay.prototype = new BMap.Overlay();
    ComplexCustomOverlay.prototype.initialize = function (map) {
        this._map = map;
        var div = this._div = document.createElement("div");
        div.style.position = "absolute";
        div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
        div.style.backgroundColor = backgroundColors[this._type];
        div.style.border = "1px solid " + borders[this._type];
        div.style.color = "white";
        div.style.height = "18px";
        div.style.padding = "2px";
        div.style.cursor = "pointer";
        div.style.lineHeight = "18px";
        div.style.whiteSpace = "nowrap";
        div.style.MozUserSelect = "none";
        div.style.fontSize = "12px"
        var span = this._span = document.createElement("span");
        div.appendChild(span);
        //调整地图楼盘初始显示价格
        span.appendChild(document.createTextNode(this._overText));
        var that = this;
        var arrow = this._arrow = document.createElement("div");
        arrow.style.background = "url(__IMG__/label.png) no-repeat";
        arrow.style.backgroundPosition = "0px " + positions[this._type] + "px";
        arrow.style.position = "absolute";
        arrow.style.width = "11px";
        arrow.style.height = "10px";
        arrow.style.top = "22px";
        arrow.style.left = "10px";
        arrow.style.overflow = "hidden";
        div.appendChild(arrow);
        div.onmouseover = function () {
            map_mouseover(div, arrow, that);
        }
        div.onmouseout = function () {
            map_mouseout(div, arrow, that);
        }
        //20和21 用于网上售楼处地图交通里面的显示, 不需要onclick事件
        if (this._y_type != 21 && this._y_type != 20 && this._y_type != 19) {
            div.onclick = function () {
                select_point = new BMap.Point(that._point_lng, that._point_lat);
                select_bid = that._bid;
                select_bname = that._text;
                select_path = that._path;
                select_address = that._address;
                select_type = that._y_type;
                select_overText = that._overText;
                select_phone1 = that._phone1;
                select_contacts = that._contacts;
                select_price = select_overText.replace(select_bname, "");
                fang_cms_oncilck();
            }
        }
        mp.getPanes().labelPane.appendChild(div);
        return div;
    }
    ComplexCustomOverlay.prototype.draw = function () {
        var map = this._map;
        var pixel = map.pointToOverlayPixel(this._point);
        this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
        this._div.style.top = pixel.y - 30 + "px";
    }
    function getType(type) {
        if (type == 2 || type == 21) {
            return 1;
        } else if (type == -1) {
            return 3;
        } else {
            return 0;
        }
    }
    function map_mouseover(div, arrow, that) {
        div.style.backgroundColor = backgroundColors[2];
        $(div).css("z-index", "9999");
        div.style.borderColor = borders[2];
        div.getElementsByTagName("span")[0].innerHTML = that._overText;
        arrow.style.backgroundPosition = "0px " + positions[2] + "px";
    }
    function map_mouseout(div, arrow, that) {
        div.style.backgroundColor = backgroundColors[that._type];
        $(div).css("z-index", "1");
        div.style.borderColor = borders[that._type];
        //鼠标移出后楼盘显示内容不变

        div.getElementsByTagName("span")[0].innerHTML = that._overText;
        arrow.style.backgroundPosition = "0px " + positions[that._type] + "px";
    }

    function get_url($k){
        if($k=='detail'){
            var url = "{:U('Rental/detail',array('id'=>'@id@'))}";
        }
        url = decodeURIComponent(url);
        url = url.replace('@id@',select_bid);
        return url;
    }

    function fang_cms_oncilck() {
        msgwindow = 1;
        var phone = "<span style='color:#FF6400'><strong>" + select_phone1 + "</strong></span>";
        var sContent = "<div style='width:400px;'>";
        sContent += "<div style='float:left;width:100px; padding:30px 10px 0px 0px;overflow:hidden;'><img src=\"" + select_path + "\" style='width:100px;height:71px;'></div>";
        sContent += "<div style='width:290px;overflow:hidden;'>";
        sContent += "<h4>";
        sContent += "<a href='"+get_url('detail')+"' target='_blank' style='color:#006699;'>" + select_bname + "</a></h4>";
        sContent += "<p style='margin:0;line-height:22px;color:#999;'>租金：<span style='color:#FF6400;'><strong>" + (select_price == '' ? '  租金' : select_price + "元/月") + "</strong></span></p>";
        sContent += "<p style='margin:0;line-height:22px;color:#999;'>押金：<span style='color:#FF6400;'><strong>" + (select_price == '' ? '  押金' : select_deposit + "元") + "</strong></span></p>";
        if (select_phone1 != "") sContent += "<p style='margin:0;line-height:22px;color:#999;'>联系电话：<span id='phone' style='color:#333;'>" + select_phone1 + "</span></p>";
        if (select_contacts != "") sContent += "<p style='margin:0;line-height:22px;color:#999;'>联系人："+select_contacts+"</p>";
        sContent += "<p style='margin:0;line-height:22px;color:#999;'>楼盘地址：<span id='address_" + select_bid + "' style='color:#333;'>" + select_address + "</span></p>";
        sContent += "<p style='color:#006699;'></p>";
        sContent += "</div>";
        sContent += "</div>";
        // 为调用后台sendMsg方法添加的bid属性
        sContent += "<input type='hidden' name='bid' id='bid' value='" + select_bid + "'/>";
        var infoWindow = new BMap.InfoWindow(sContent);  // 创建信息窗口对象
        mp.openInfoWindow(infoWindow, select_point);      // 打开信息窗口
    }

    function fang_cms_map(lnt, lat, bid, bname, path, address, phone1, contacts, price,deposit) {
        select_point = new BMap.Point(lnt, lat);
        select_bid = bid;
        select_bname = bname;
        select_path = path;
        select_address = address;
        select_phone1 = phone1;
        select_contacts = contacts;
        select_price = price;
        select_deposit = deposit;
        mp.panTo(select_point);
        fang_cms_oncilck();
    }
</script>
<script type="text/javascript">
    //初始化，页面都加载完后，获取已经加载好的默认第一个城市下的子区域的楼盘和移动到该区域，并且获取地图上的标签。
    $(function () {
        HousesAjax();
        $("#area_id").change(function () {
            $("#key").val('请输入楼盘名称搜索');
            getAreaCate();
        });
        $("#area_ids,#room,#hall,#toilet,#kitchen").change(
                function () {
                    $("#key").val('请输入楼盘名称搜索');
                    HousesAjax();
                }
        );

        $(".mapSort").click(function () {
            var did = $(this).attr('data-did');
            if(did%2==0){
                var n_v = parseInt(did)-1;
                $(this).attr('data-did',n_v);
            }else{
                var n_v = parseInt(did)+1;
                $(this).attr('data-did',n_v);
            }
            $("#sort").val(n_v);
            HousesAjax();
        });
    });

    function getAreaCate(){
        $.ajax({
            type: "post",
            url: "{:U('Ajax/getAreaCate')}",
            data: {pid:$("#area_id").val()},
            dataType: "json",
            success: function (data) {
                $("#area_ids").empty();
                var html = '';
                if(data.list.length>0){
                    $(data.list).each(function (i, v) {
                        html += "<option value='" + v.id + "'>" + v.name + "</option>";
                    });
                }else{
                    html += "<option value='0'>暂无分类</option>";
                }
                $("#area_ids").append(html);
                HousesAjax();
            }
        })
    }
    function HousesAjax() {
        var key = $("#key").val();
        if(key=='请输入楼盘名称搜索'){
            key = '';
        }
        $.ajax({
            type: "post",
            url: "{:U('Ajax/getUsedRentalList')}",
            data: {area_id:$("#area_id").val(),area_ids:$("#area_ids").val(),room:$("#room").val(),hall:$("#hall").val(),kitchen:$("#kitchen").val(),toilet:$("#toilet").val(),key:key,sort:$("#sort").val(),type:'rental'},
            dataType: "json",
            success: function (json) {
                var data = json.data;
                getTemplate(data.d);
                var arrHouseID = houseID.substring(0, houseID.length - 1).split(','); //将已经加载的楼盘的ID字符串转成数组

                $(data.d).each(function (i, ite) {
                    if (!arrHouseID.contains(ite.id.toString())) {
                        var txt = ite.title, mouseoverTxt = txt + " " + ite.price;
                        var mapVal = ite.map.split(',');
                        var myCompOverlay = new ComplexCustomOverlay(mapVal[0], mapVal[1], ite.title, mouseoverTxt, 1, ite.id, ite.address, ite.img, ite.phone, ite.contacts);
                        mp.addOverlay(myCompOverlay);
                        houseID += ite.id.toString() + ","; //保存已经加载的楼盘ID

                    }
                })
                if (data.d.length > 0) {
                    var mapVal = data.d[0].map.split(',');
                    fang_cms_map(mapVal[0], mapVal[1], data.d[0].id, data.d[0].title, data.d[0].img, data.d[0].address, data.d[0].phone,data.d[0].contacts,data.d[0].price,data.d[0].deposit);
                }
                $(".mapList").scrollTop(0);
                $("#HousesCount").html(data.d.length);
            }
        });
    }

    function getTemplate(data){
        var html = '';
        $(data).each(function (i, v) {
            var mapVal = v.map.split(',');
            html += "<li onclick=\"fang_cms_map('" + mapVal[0] + "','"+mapVal[1]+"','"+v.id+"','"+v.title+"','"+ v.img+"','"+v.address+"','"+ v.phone+"','"+v.contacts+"','"+v.price+"','"+v.deposit+"')\">";
            html += "<a href='javascript:void(0)'>";
            html += "<h2>"+v.title+"</h2><dl><dt>位置: "+v.address+"</dt><dd><span>租金：<b>"+v.price+"元</b></span></dd></dl></a></li>";
        });
        $("#ulContent").empty();
        $("#ulContent").append(html);
    }

</script>
</body>
</html>